﻿using System;
using System.Diagnostics.Contracts;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet
{
    class CellExpressionFormula: ExpressionFormula
    {
        private readonly Cell _target;

        public CellExpressionFormula(Expression expression, Cell target) : base(expression)
        {
            Contract.Requires(target != null);
            this._target = target;
        }

        protected override Worksheet Worksheet
        {
            get { return _target.Worksheet; }
        }
        internal override object GetValue()
        {
            return Expression.GetValue(new CellEvaluationContext(_target));
        }

        public override CellFormulaBase Offset(CellRelativeAddress cellRelativeAddress)
        {
            return new CellExpressionFormula(this.Expression.Offset(cellRelativeAddress), _target);
        }
    }
}